{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "6种类型：\n",
    "\n",
    "Number（数字）、String（字符串）、Tuple（元组）、List（列表）、Set（集合）、Dictionary（字典）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 列表\n",
    "\n",
    "创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建\n",
      "[1, 2, 3, 4, 5]\n",
      "[1, 'hello', 2.5, [1, 3, 5]]\n",
      "['w', 'o', 'r', 'l', 'd']\n",
      "[0, 1, 2, 3, 4]\n",
      "[2, 3, 4]\n",
      "[1, 3]\n"
     ]
    }
   ],
   "source": [
    "print('创建')\n",
    "a=[1,2,3,4,5] #中括号!\n",
    "print(a)\n",
    "b=[1,'hello',2.5,[1,3,5]] #列表中元素类型可以不一样\n",
    "print(b)\n",
    "c=list('world') #转换为列表\n",
    "print(c)\n",
    "#内置的range函数产生列表\n",
    "print((list(range(5))))\n",
    "print((list(range(2,5))))\n",
    "print((list(range(1,5,2))))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "基本使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 100)\n",
      "[1, 2, 3, 4, 100]\n"
     ]
    }
   ],
   "source": [
    "print((a[0],a[-1]))\n",
    "a[4]=100\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3]\n",
      "[2, 3, 4, 100]\n",
      "[1, 2, 3]\n",
      "[2, 3]\n",
      "[3, 4, 100]\n"
     ]
    }
   ],
   "source": [
    "print((a[1:3])) #前闭后开[)\n",
    "print((a[1:]))\n",
    "print((a[:3]))\n",
    "print((a[1:-2]))\n",
    "print((a[-3:]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "100\n",
      "2500\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "print((len(a)))\n",
    "print((max(a)))\n",
    "print((sum(range(1,101,2))))\n",
    "e=[1,2,1,1,2]\n",
    "print((e.count(1)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "增"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 100, 4]\n",
      "[1, 2, 3, 4, 100, 4, 'w', 'o', 'r', 'l', 'd']\n",
      "[1, 2, 3, 3, 4, 100, 4, 'w', 'o', 'r', 'l', 'd']\n"
     ]
    }
   ],
   "source": [
    "a.append(4) #在最后追加一个元素\n",
    "print(a)\n",
    "a.extend(c) #将c追加到a后面\n",
    "print(a)\n",
    "a.insert(2,3)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "删"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 100, 4, 'w', 'o', 'r', 'l', 'd']\n",
      "[1, 2, 3, 4, 100, 4, 'w', 'o', 'r', 'l']\n",
      "[1, 2, 3, 100, 4, 'w', 'o', 'r', 'l']\n",
      "[1, 2, 3, 100]\n"
     ]
    }
   ],
   "source": [
    "a.pop(2)\n",
    "print(a)\n",
    "x=a.pop()\n",
    "print(a)\n",
    "a.remove(4) #只移除第一个\n",
    "print(a)\n",
    "del a[4:]\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 100, 5, 6, 7]\n",
      "[5, 6, 7, 5, 6, 7, 5, 6, 7]\n",
      "[100, 3, 2, 1]\n",
      "[1, 2, 3, 100]\n"
     ]
    }
   ],
   "source": [
    "f=list(range(5,8))\n",
    "print((a+f)) #合并\n",
    "print((f*3)) #重复\n",
    "a.reverse()\n",
    "print(a)\n",
    "a.sort()\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "查"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "2\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print(([1,2,2,2,2,3,3].count(2)))\n",
    "d=['How','are','you']\n",
    "print((d.index('you')))\n",
    "print(('me' in d))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "列表做队列使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "deque(['are', 'you', 'fine'])\n"
     ]
    }
   ],
   "source": [
    "from collections import deque\n",
    "q = deque(d)\n",
    "q.append('fine')\n",
    "q.popleft()\n",
    "print(q)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3)\n",
      "(1, 2, 3, 4)\n",
      "(5,)\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-13-4e165dc550c2>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m4\u001b[0m \u001b[1;31m#不能修改\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "a=(1,2,3) #小括号!\n",
    "print(a)\n",
    "print((tuple([1,2,3,4]))) #将列表[1,2,3,4]强制转化为元组\n",
    "b=5,\n",
    "print(b)\n",
    "a[1]=4 #不能修改"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字典\n",
    "\n",
    "创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'math': 100, 'physics': 80, 'computer': 90}\n",
      "{'english': 60, 'art': 40}\n",
      "{'math': 100, 'physics': 100, 'computer': 100}\n"
     ]
    }
   ],
   "source": [
    "a={'math':100, 'physics': 80, 'computer':90} #字典为大括号\n",
    "print(a)\n",
    "items=[('english',60), ('art',40)]\n",
    "b=dict(items) #dict为字典的关键字\n",
    "print(b)\n",
    "c=dict.fromkeys(['math','physics','computer'], 100) #keys共用的value\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "增"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'math': 100, 'physics': 80, 'computer': 90, 'history': 85}\n"
     ]
    }
   ],
   "source": [
    "a['history']=85\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "删"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'math': 100, 'physics': 80, 'computer': 90}\n"
     ]
    }
   ],
   "source": [
    "a.pop('history') #必须有该key，否则异常。返回删除的value\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'english': 60, 'art': 50}\n",
      "{'english': 60, 'art': 50, 'math': 100, 'physics': 80, 'computer': 90}\n"
     ]
    }
   ],
   "source": [
    "b['art']=50\n",
    "print(b)\n",
    "b.update(a)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "查"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "None 0\n"
     ]
    }
   ],
   "source": [
    "print(('math' in a))\n",
    "print(a.get('philosophy'), a.get('philosophy',0))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "遍历"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['math', 'physics', 'computer']\n",
      "[100, 80, 90]\n",
      "[('math', 100), ('physics', 80), ('computer', 90)]\n"
     ]
    }
   ],
   "source": [
    "print((list(a.keys())))\n",
    "print((list(a.values())))\n",
    "print((list(a.items())))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "缺省字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "from collections import defaultdict\n",
    "dic = defaultdict(int)\n",
    "print((dic['a']))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有序字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a A\n",
      "b B\n",
      "c C\n",
      "d D\n"
     ]
    }
   ],
   "source": [
    "from collections import OrderedDict\n",
    "d1 = OrderedDict()  #将普通字典转换为有序字典\n",
    "d1['a'] = 'A'\n",
    "d1['b'] = 'B'\n",
    "d1['c'] = 'C'\n",
    "d1['d'] = 'D'\n",
    "for k,v in d1.items():\n",
    "    print(k,v)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 集合\n",
    "\n",
    "定义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'b', 'a', 'c'} {'b', 'd', 'c'} set()\n"
     ]
    }
   ],
   "source": [
    "x = set('abc')\n",
    "y = {'b','c','d'}\n",
    "z = set() #空集合不能用{}定义\n",
    "print(x,y,z)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'b', 'c'}\n",
      "{'a', 'd', 'c', 'b'}\n",
      "{'a'}\n",
      "{'d', 'a'}\n"
     ]
    }
   ],
   "source": [
    "print((x&y))\n",
    "print((x|y))\n",
    "print((x-y))\n",
    "print((x^y))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print((len(x)))\n",
    "print(('a' in x))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a'}\n"
     ]
    }
   ],
   "source": [
    "z = set()\n",
    "z.add('a')\n",
    "z.update('b') #参数还可以是列表，元组，字典等\n",
    "z.add('e')\n",
    "z.remove('e') #或discard(不存在不报错)\n",
    "z.pop() #删除任意一个元素\n",
    "print(z)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "查询"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print((z.issubset(x)))\n",
    "print((z<x))\n",
    "print((x.issuperset(z)))\n",
    "print((x<y or x==y))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "不可变集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "frozenset({1, 2, 3})\n"
     ]
    },
    {
     "ename": "AttributeError",
     "evalue": "'frozenset' object has no attribute 'add'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-29-702a58e86729>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0ms\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mfrozenset\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0ms\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m: 'frozenset' object has no attribute 'add'"
     ]
    }
   ],
   "source": [
    "s = frozenset([1,2,3])\n",
    "print(s)\n",
    "s.add(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
